package com.hkp.demo.service.impl;

import com.hkp.demo.entity.AuditResult;
import com.hkp.demo.entity.vo.DimensionGroupVO;
import com.hkp.demo.entity.vo.ResultDayGroupVO;
import com.hkp.demo.entity.vo.RuleQualityVO;
import com.hkp.demo.mapper.AuditResultMapper;
import com.hkp.demo.service.IAuditResultService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 稽核结果表 服务实现类
 * </p>
 *
 * @author HuKangPing
 * @since 2023-02-22 14:20:34
 */
@Service
@Transactional(readOnly = true, rollbackFor = Exception.class)
public class AuditResultServiceImpl extends ServiceImpl<AuditResultMapper, AuditResult> implements IAuditResultService {

    @Resource
    private AuditResultMapper auditResultMapper;

    @Override
    public Long getAllAuditNum(String day, String result) {
        return auditResultMapper.getAllAuditNum(day, result);
    }

    @Override
    public Long getCurrentDayAuditNum(String day, String result) {
        return auditResultMapper.getCurrentDayAuditNum(day, result);
    }

    @Override
    public List<DimensionGroupVO> getDimensionGroupVO(String day) {
        return auditResultMapper.getDimensionGroupVO(day);
    }

    @Override
    public List<ResultDayGroupVO> getResultDayGroupVO(String day) {
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd");
//          得到选中的日期
        LocalDate endDate = LocalDate.parse(day, df);
        LocalDate startDate = endDate.minusDays(29);
        String startDay = df.format(startDate);
//          接口结果列表
        List<ResultDayGroupVO> resultDayGroupVOList = new ArrayList<>(30);
//          遍历从开始那天的一个月内每天的稽查总数，警告数，和严重数
        for (int i = 0; i < 30; i++) {
//              得到MM-dd
            String s = startDay.substring(startDay.indexOf("-") + 1);
//              得到MM/dd（resultDay）
            String resultDay = s.replace("-", "/");
            Long alertCount = getCurrentDayAuditNum(startDay, "2");
            Long severeCount = getCurrentDayAuditNum(startDay, "3");
            Long checkCount = getCurrentDayAuditNum(startDay, null);
            resultDayGroupVOList.add(new ResultDayGroupVO(resultDay, severeCount, alertCount, checkCount));
            startDate = startDate.plusDays(1);
            startDay = df.format(startDate);
        }
        return resultDayGroupVOList;
    }

    /**
     * 去除当天没有警告和严重的
     *
     * @param day
     * @return
     */
    @Override
    public List<RuleQualityVO> getRuleQualityVO(String day) {
        List<RuleQualityVO> ruleQualityVOList = auditResultMapper.getRuleQualityVO(day);
//        过滤当天没有警告和严重的
        List<RuleQualityVO> RuleQualityVOS = ruleQualityVOList.stream().filter(ruleQualityVO -> ruleQualityVO.getAlertCount() != 0 || ruleQualityVO.getSevereCount() != 0)
                .collect(Collectors.toList());
        return RuleQualityVOS;
    }

    @Override
    public List<String> getListExecCalendar(String month) {
        return auditResultMapper.getListExecCalendar(month);
    }
}
